#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Project : ailearning
@File : debug.py
@Author : 李峰
@Date : 2025/10/15 9:43
@Software: PyCharm
'''
import re
s = "[{lifecycle=1.1444, wait=0.349363, test=0.795035, level=二级, name=杨哲聪, count=1, target=1.3, Interval=截止到2025-10-14}, {lifecycle=0.935191, wait=0.48912, test=0.446071, level=三级, name=詹玉莲, count=3, target=2.4, Interval=截止到2025-10-14}, {lifecycle=1.8964, wait=1.8774, test=0.0190046, level=三级, name=谢相莹, count=1, target=2.4, Interval=截止到2025-10-14}]"


# from typing import List, Dict, Any
# s = vars.get("all_list")
# log.info(s)

# 1. 提取每个 {} 内的原始键值对字符串
raw_blocks = re.findall(r'\{([^}]+)\}', s)
# log.info(str(raw_blocks))
# 2.新建空[],存放解析的字典
data = []
# 2. 把每个键值对拆成字典
for block in raw_blocks:
    # log.info(type(block))
    # 匹配：key=值，值可能是数字或中文/日期
    pairs = re.findall(r'(\w+)=(.+?)(?:,|$)', block)
    d = {}
    for k, v in pairs:
        v = v.strip()
        # 尝试转 float
        try:
            v = float(v)
        except ValueError:
            pass
        d[k] = v
    data.append(d)
# log.info(str(data))
print(data)
if not data:
    vars.put("dingding_message", "截止到当前暂无数据")
else:
    # 表头（中文）
    headers = ["姓名", "等级", "需求数量", "测试等待耗时", "测试验证耗时", "测试周期", "目标值"]
    # 对应字段名
    fields = ["name", "level", "count", "wait", "test", "lifecycle", "target"]
    interval='2025-10-14'
    # 组装表头
    md_lines = u"本季度截止到：{}，一二三级业务需求交付效率详细如下：".format(interval)+"",
        "|" + "|".join(headers) + "|",
        "|" + "|".join(["---"] * len(headers)) + "|"
    ]
    # log.info(md_lines)
    print(md_lines)
    # 逐行写数据
    for row in data:
        values = []
        for f in fields:
            # 数字保留 3 位小数，避免过长
            val = row.get(f, "")
            if isinstance(val, float):
                val = u"{:.3f}".format(val)
            values.append(str(val))
        md_lines.append(u"|" + u"|".join(values) + u"|")
    # log.info(str(md_lines))
    print(md_lines)